#
# author: hone
#

CC      = gcc

CFLAGS += -g
CFLAGS += -Wall
#CFLAGS += -O2   # if used, error msg: kernel: echosvr[16734]: segfault at 7fffff6a ip 00007f1aa7779b5b sp 00007f1aa7f1c970 error 4 in libc-2.17.so[7f1aa7731000+1b6000]
CFLAGS += -D_REENTRANT
CFLAGS += -fPIC

#CFLAGS += -Wno-unused-variable

DEST   = echosvr
DEST_O = main.o yhevent.o error.o daemon_init.o yhsocket.o arguments.o

LIBOUT = libyhevent.so
LIBOUT_O = yhevent.o error.o

#LIBS += -Wl,-rpath,./lib -L./lib -lyhsocket -lpthread 
LIBS += -Wl,-rpath,./lib -L./lib -lpthread 

HEAD = yhevent.h yhsocket.h
OBJS = main.o yhevent.o error.o daemon_init.o 

#strip $(LIBOUT)

## 信息显示控制选项
HINT = @
CONT = @
CCOM = $(HINT) printf '\e[31mCC\e[32m   %s \e[33m into \e[m %s\n' $< $@
LINK = $(HINT) printf '\e[31mLINK\e[32m %s \e[33m from \e[m ' $@ && echo $^

## 编译命令 不使用新编译出来的动态库
COMMON = $(CONT) $(CC) $^ $(CFLAGS) $(LIBS)-o $@

ifeq "${v}" "y"
	CCOM =
	LINK =
	CONT =
endif

.c.o:
	$(CCOM)
	$(CONT) $(CC) -c $(CFLAGS) $< -o $@ 

all: $(DEST)

$(LIBOUT): $(LIBOUT_O)
	$(LINK)
	$(CONT) $(CC) $(CFLAGS) $(LIBOUT_O) -shared -fPIC -Wl,-rpath,./ -L./ -lpthread -o $(LIBOUT) 

$(DEST): $(DEST_O)
	$(LINK)
	$(COMMON)

lib: $(LIBOUT)
	mkdir -p lib include && cp $(LIBOUT) lib && cp yhsocket.h yhevent.h include

clean:
	rm $(DEST) $(DEST_O) $(LIBOUT) $(OBJS) -rf

install: $(DEST)
	cp $(DEST) /usr/bin
uninstall: clean
	rm -rf /usr/bin/$(DEST)
